<template>
  <!--页面搭建-杜思成-->
  <layout-card
      v-model="search"
      :filters="exampleOptions"
      headline="信息列表"
      headtitle="数据查询"
      :hasButton="true"
      @search-change="pageInfo"
      @search-reset="searchReset">
    <template #action>

    </template>
    <table-common
        stripe="true"
        :columns="exampleColumns"
        :loading="loading"
        :pagination="pager"
        :table-data="list"
        size="large"
        @current-change="changeCurrent"
        @size-change="changePageSize"
        @sort-change="sortChange">
    </table-common>
  </layout-card>
</template>
<script lang="ts" setup>
import {ref, reactive, onMounted, computed} from 'vue'
// 导入对应方法
import {page, downloadFile} from '@/api/pax/deposit'
// 7-18修改LayoutCard 页面，hasButton参数为false时显示为弹窗样式，为true时显示为搜索栏样式
import LayoutCard from '@/components/LayoutCard/index.vue'
import TableCommon from '@/components/TableCommon/index.vue'
import {exampleColumns} from '@/views/quality/attendanceManger/config/columns'
import {exampleOptions} from '@/views/quality/attendanceManger/config/filters'
import {ElMessage} from 'element-plus'
import {
  getEmployeeClock
} from "@/api/quality/attendanceManger";
import moment from 'moment'
import {getResource} from "@/api/resource";
import {getPostList} from "@/api/quality/scheduleGroup";
import {useStore} from "vuex";
const {getters} = useStore();
const organizes=computed(() => getters['user/getAllOrganTree']);
// 存储获取回来的值
const list = ref([])
const loading = ref(false)
// 分页
const pager = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0,
  pageCount: 1
})
// 排序
const order = ref([
  {
    column: 'emp.staff_number',
    method: 'desc'
  }
])
const search = ref()
const condition=ref({
  startTime:'',
  endTime:'',
  name:''
})
/**
 * @functionName 分页功能
 * @param {number} val
 * @description
 * @author 陈振兵
 * @date 2023-07-21 17:29:28
 * @version V1.0.0
 * @return
 */
const changeCurrent = (val: number) => {
  pager.currentPage = val
  getList()
}
// 每页展示条数发生变化
const changePageSize = (val: number) => {
  pager.currentPage = 1;
  pager.pageSize = val;
  order.value = [
    {
      column: 'emp.staff_number',
      method: 'desc'
    }
  ];// 清空排序？？？
  getList()
}
/**
 * @functionName 排序事件
 * @param {any} data 传入的某一列的列名和升降序
 * @description
 * @author 蒋好东
 * @date 2023-07-21 17:26:19
 * @version V1.0.0
 * @return
 */
const sortChange = (data: any) => {
  console.log(data);
  pager.currentPage = 1;
  const method = data.order === 'descending' ? 'desc' : 'asc';
  order.value = [{
    column:data.prop == 'staffNumber' ?'emp.staff_number':'det.schedule_date', method,
  }];
  getList()
}
/**
 * @functionName 分页查询
 * @author 蒋好东
 * @date 2023-07-21 17:27:48
 * @version V1.0.0
 * @return
 */
const pageInfo = async (val: any) => {
  let startTime, endTime;
  if (val.date && val.date.length > 0) {
    condition.value.startTime = moment(val.date[0]).format("YYYY-MM-DD") ;
    condition.value.endTime = moment(val.date[1]).format("YYYY-MM-DD");
  }else{
    condition.value.startTime=''
    condition.value.endTime=''
  }
  condition.value.name=val.name
  pager.currentPage = 1;
  getList()
}
/**
 * @functionName 重置方法
 * @param {any} data
 * @description
 * @author 蒋好东
 * @date 2023-07-21 17:21:20
 * @version V1.0.0
 * @return
 */
const searchReset = (data: any) => {
  condition.value.startTime=''
  condition.value.endTime=''
  condition.value.name=''
  pager.currentPage = 1;
  list.value = []
  getList()
}

// 获取表格数据
const getList = async () => {
  loading.value = true;
  const payload = {
    pageNum: pager.currentPage,
    pageSize: pager.pageSize,
    condition: {
      name: condition.value?.name,
      startTime: condition.value?.startTime,
      endTime: condition.value?.endTime,
      order:order.value
    }
  };
  list.value = []
  const res = await getEmployeeClock(payload)
  if (res?.code == '1') {
    if (res.data && res.data.list.length > 0) {
      list.value = res.data.list
      list.value.forEach(item => {
        postList.value.forEach(item1 => {
          if (item1.positionId == item.positionId) {
            item.positionName = item1.name
          }
        })
        if(item.organ)item.organId=JSON.parse(item.organ).organId
        ergodicTree(organizes.value,item)
      })
      pager.total = res.data.total;
      pager.pageCount =
          Math.ceil(pager.total / pager.pageSize) === 0
              ? 1
              : Math.ceil(pager.total / pager.pageSize);
    }
  }
  loading.value = false;
}
const postList=ref([])
const onGetResource= async () => {
  const res1 = await getPostList({
    state: "VALID"
  })
  if (res1?.code === '1') {
    postList.value = res1.data
  } else {
    ElMessage.error(res1?.message);
  }
  getList()
};
const ergodicTree = (datas: any,item1:any) => {
  for (let item of datas) {
    if(item.organizationId==item1.organId){
      item1.organName=item.organizationName
    }else{
      if (item.children && item.children.length > 0) {
        ergodicTree(item.children,item1)
      }
    }
  }

};
onMounted(() => {
  onGetResource()
})
</script>
